/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.utils;

import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JTable;

import javax.swing.RowSorter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;

/**
 *
 * @author xmarti
 */
public abstract class JTableXM extends JTable {  
    private List<RowSorter.SortKey> orderTable=null;
    public JTableXM(){
        super();

        this.selectionModel.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {                  
                    if(!e.getValueIsAdjusting())    handleSelectionEvent(e);        
                }
            });        
        this.setAutoCreateRowSorter(true);
    }
    public JTableXM(TableModel model){
        super();  
        this.selectionModel.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {                    
                    if(!e.getValueIsAdjusting())handleSelectionEvent(e);        
                    }
                });        
        
        this.setAutoCreateRowSorter(true);
        this.setModel(model);
    }
    public void chargeModel (TableModel model){
        this.setModel(model);
        this.getRowSorter().addRowSorterListener(new RowSorterListener() {
            @Override
            public void sorterChanged(RowSorterEvent e) {
                if(!e.getSource().getSortKeys().isEmpty())
                    orderTable=(List<RowSorter.SortKey>) e.getSource().getSortKeys();
            }
        });
        if(this.orderTable!=null) this.getRowSorter().setSortKeys(this.orderTable);
       // this.selectionModel = this.getSelectionModel();     // passem el listener per quan hi ha una selecció   
        
        
        
    }
    public int getRowCount(){
        return this.getModel().getRowCount();
    }
    public Object getObject(){             
        return this.getValueAt(this.getSelectedRow(),0);
    }
    public void setScroll(){
        this.scrollRectToVisible(new Rectangle(this.getCellRect(this.getSelectedRow(), 0, true)));                                   
    }
    public int getIndex(Object  entity){                
        for(int i =0;i< this.getRowCount();i++){
            if(this.getValueAt(i, 0).toString().equals(entity.toString())){ 
                return i;
            } 
        }
        return -1;
    }        
    public abstract void handleSelectionEvent(ListSelectionEvent e);
}
